
from PyQt5.QtWidgets import QWidget, QLabel
from PyQt5.QtCore import QPropertyAnimation, QRect, QTimer, QEasingCurve, QAbstractAnimation


class scrolltext(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.textlable = QLabel(self)
        self.textlable.setStyleSheet("QLabel {"
                                     "font-family: \"Microsoft YaHei\";"
                                     "font-size: 18px;"
                                     "color: red;"
                                     "font-style: normal;"
                                     "font-weight: bold;}")
        self.textlable.setText("This is scrolltext")
        self.textlable.setFixedHeight(20)
        self.textlable.hide()

        # 文字上移特效
        self.m_TopPropertyAnimation = QPropertyAnimation(self, b"geometry")
        self.m_TopPropertyAnimation.setDuration(600)

    def animationStart(self):
        if self.textlable.isHidden():
            self.textlable.show()
        self.m_TopPropertyAnimation.setStartValue(QRect(0, self.textlable.height(), self.textlable.width(), self.textlable.height()))
        self.m_TopPropertyAnimation.setEndValue(QRect(0, 0, self.textlable.width(), self.textlable.height()))
        self.m_TopPropertyAnimation.setEasingCurve(QEasingCurve.OutQuart)
        self.m_TopPropertyAnimation.start(QAbstractAnimation.KeepWhenStopped)

        timer = QTimer(self)
        timer.start(3000)

        timer.timeout.connect(lambda: self.onTimeout(timer))

    def onTimeout(self, timer):
        self.m_TopPropertyAnimation.setStartValue(QRect(0, 0, self.textlable.width(), self.textlable.height()))
        self.m_TopPropertyAnimation.setEndValue(QRect(0, self.textlable.height(), self.textlable.width(), self.textlable.height()))
        self.m_TopPropertyAnimation.start(QAbstractAnimation.KeepWhenStopped)
        timer.stop()